// API服务层 - 统一管理所有API调用
import { http } from '../utils/http'
import { API_URLS } from '../config/api'

// 数据类型定义
export interface Post {
  id: number
  summary: string
  writer: string
  updateTime: string
  likeNum?: number
  commentNum?: number
}

export interface PostDetail {
  id: number
  summary: string
  content: string
  writer: {
    id: number
    name: string
  }
  updateTime: string
  likeNum: number
  saveNum: number
  commentNum: number
  isLiked: boolean
  isSaved: boolean
}

export interface User {
  id: number
  name: string
  type: number
  followNum: number
  followerNum: number
  postNum: number
}

export interface Product {
  id: number
  name: string
  price: number
  originalPrice: number
  description: string
  seller: string
  image: string
  condition: string
  category: string
}

export interface LoginRequest {
  username: string
  password: string
}

export interface RegisterRequest {
  username: string
  password: string
  email: string
}

export interface CreatePostRequest {
  summary: string
  content: string
}

export interface CreateProductRequest {
  name: string
  price: number
  originalPrice: number
  description: string
  condition: string
  category: string
  images: string[]
}

export interface PaginationParams {
  page?: number
  pageSize?: number
}

export interface PostListParams extends PaginationParams {
  sortBy?: string
  sortOrder?: string
}

export interface ProductListParams extends PaginationParams {
  category?: string
}

export interface PaginatedResponse<T> {
  data: T[]
  total: number
  page: number
  pageSize: number
}

// 用户认证相关API
export const authApi = {
  // 用户登录
  login: (data: LoginRequest) => {
    return http.post<{ userId: number; username: string; token: string }>(
      API_URLS.AUTH_LOGIN,
      data
    )
  },

  // 用户注册
  register: (data: RegisterRequest) => {
    return http.post<{ userId: number; username: string; token: string }>(
      API_URLS.AUTH_REGISTER,
      data
    )
  }
}

// 帖子相关API
export const postsApi = {
  // 获取帖子列表
  getList: (params: PostListParams) => {
    return http.get<{ posts: Post[]; total: number; page: number; pageSize: number }>(
      API_URLS.POSTS_LIST,
      params
    )
  },

  // 获取帖子详情
  getDetail: (id: number) => {
    return http.get<PostDetail>(API_URLS.POSTS_DETAIL(id))
  },

  // 创建帖子
  create: (data: CreatePostRequest) => {
    return http.post<{ postId: number }>(API_URLS.POSTS_CREATE, data)
  },

  // 点赞帖子
  like: (id: number) => {
    return http.post<{ isLiked: boolean; likeNum: number }>(
      API_URLS.POSTS_LIKE(id)
    )
  },

  // 收藏帖子
  save: (id: number) => {
    return http.post<{ isSaved: boolean; saveNum: number }>(
      API_URLS.POSTS_SAVE(id)
    )
  }
}

// 用户相关API
export const usersApi = {
  // 获取用户信息
  getInfo: (id: number) => {
    return http.get<User>(API_URLS.USER_INFO(id))
  },

  // 获取用户发布的帖子
  getPosts: (id: number, params: PaginationParams) => {
    return http.get<{ posts: Post[]; total: number }>(
      API_URLS.USER_POSTS(id),
      params
    )
  },

  // 关注用户
  follow: (id: number) => {
    return http.post<{ isFollowed: boolean; followerNum: number }>(
      API_URLS.USER_FOLLOW(id)
    )
  }
}

// 跳蚤市场相关API
export const marketApi = {
  // 获取商品列表
  getProducts: (params: ProductListParams) => {
    return http.get<{ products: Product[]; total: number; page: number; pageSize: number }>(
      API_URLS.MARKET_PRODUCTS,
      params
    )
  },

  // 创建商品
  createProduct: (data: CreateProductRequest) => {
    return http.post<{ productId: number }>(API_URLS.MARKET_PRODUCTS, data)
  }
}

// 统一导出所有API
export const api = {
  auth: authApi,
  posts: postsApi,
  users: usersApi,
  market: marketApi
}

export default api